package com.shanghaionstar.user.service;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.onstar.cn.api.UsersService;
import com.onstar.cn.auth.userProfile.Vehicle;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.smart.BaseResponse;
import com.onstar.cn.smart.entity.ServiceItem;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Yonglang Hong on 2016/9/26.
 */
public class OptService extends BaseService{
    private Context context;

    public OptService(Context context) {
        this.context = context;
    }

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public Object jsonToBean(String json) {
        return null;
    }

    public Observable<BaseResponse> optInService(final String serviceName) {
        return Observable.defer(new Func0<Observable<BaseResponse>>() {
            @Override
            public Observable<BaseResponse> call() {
                try {
                    return Observable.just(doOptInService(serviceName));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }


    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.POST)
    public BaseResponse doOptInService(String serviceName) throws OnStarException {
        String idpId = ApplicationUtil.getInstance().getIdpID();
        String accountID = ApplicationUtil.getInstance().getAccountId();
        String vin = ApplicationUtil.getInstance().getVin();

        UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        OptEntity optEntity = new OptEntity();
        optEntity.setServiceName(serviceName);
        String body = GsonHelper.toJson(optEntity);
        service.optInService(requestEntity, idpId, accountID,vin,body);
        requestEntity = HttpInjectUtils.injectHttp(getClass(), "doOptInService", requestEntity,String.class);
        String responseStr = NetConnectUtil.getResponse(getDatas(requestEntity));
        BaseResponse passResponse = GsonHelper.fromJson(responseStr,BaseResponse.class);
        return passResponse;
    }

    public Observable<BaseResponse> optOutService(final String serviceName) {
        return Observable.defer(new Func0<Observable<BaseResponse>>() {
            @Override
            public Observable<BaseResponse> call() {
                try {
                    return Observable.just(doOptOutService(serviceName));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.DELETE)
    public BaseResponse doOptOutService(String serviceName) throws OnStarException {
        String idpId = ApplicationUtil.getInstance().getIdpID();
        String accountID = ApplicationUtil.getInstance().getAccountId();
        String vin = ApplicationUtil.getInstance().getVin();

        UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
//        String body = JsonUtils.object2Json(new OptEngtity(),false);SmartDrive
        service.optOutService(requestEntity, idpId, accountID,vin,serviceName);
        LinkedHashMap<String, Object> coHashMap = new LinkedHashMap<>();
        coHashMap.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(coHashMap);
        requestEntity = HttpInjectUtils.injectHttp(getClass(), "doOptOutService", requestEntity,String.class);
        String responseStr = NetConnectUtil.getResponse(getDatas(requestEntity));
        BaseResponse passResponse = GsonHelper.fromJson( responseStr,BaseResponse.class);
        return passResponse;
    }

    public Observable<List<ServiceItem>> getServiceStatus(final String serviceName) {
        return Observable.defer(new Func0<Observable<List<ServiceItem>>>() {
            @Override
            public Observable<List<ServiceItem>> call() {
                try {
                    List<ServiceItem> items = new Gson().fromJson(getServiceStatusJson(serviceName), new TypeToken<ArrayList<ServiceItem>>() {
                    }.getType());
                    return Observable.just(items);
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public String getServiceStatusJson(String serviceName) throws OnStarException {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        service.getServiceStatus(requestEntity, ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin(), serviceName);
        requestEntity = HttpInjectUtils.injectHttp(OptService.this.getClass(), "getServiceStatusJson", requestEntity, String.class);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    public class OptEntity{
        private String serviceName = "SmartDrive";

        public String getServiceName() {
            return serviceName;
        }

        public void setServiceName(String serviceName) {
            this.serviceName = serviceName;
        }
    }
}
